package com.sltx.controller;

import com.sltx.controller.base.BaseController;
import com.sltx.service.ExcelService;
import com.sltx.util.ImportExcel;
import com.sltx.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/excel")
public class ExcelController extends BaseController {

    @Autowired
    private ExcelService service;

    /**
     * 税收数据上传
     * @param file
     * @return
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object getEntNameNum(@RequestParam MultipartFile file){
        PageData pd = this.getPageData();
        try {
            //获取上传的文件名
            String fileName = file.getOriginalFilename();
            // 获取上传文件的输入流
            InputStream inputStream = file.getInputStream();
            // 调用工具类中方法，读取excel文件中数据
            List<Map<String, Object>> list = ImportExcel.readExcel(fileName, inputStream);
            Object o = service.insertExcel(list, pd);
            return o;
        }catch (Exception e){
            return e;
        }

    }


    /**
     * 查看税收表
     *
     * @return
     */
    @RequestMapping("/selectEntQyss")
    @ResponseBody
    public Object selectEntQyss(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        if ("".equals(pd.get("year"))) {
            pd.remove("year");
        }
        result.put("Qyss",service.selectEntQyss(pd));
        result.put("year",service.Entyear(pd));
        return result;
    }


    /**
     * 查看月份缴税详情
     * @param request
     * @return
     */
    @RequestMapping("/getEntmonth")
    @ResponseBody
    public Object Entmonth(HttpServletRequest request) {
        PageData pd = this.getPageData();
        List<Map> result = new ArrayList<>();
        result = service.Entmonth(pd);
        return result;

    }


    /**
     * 修改税收金额
     * @param request
     * @return
     */
    @RequestMapping("/updateSkje")
    @ResponseBody
    public Object updateSkje(HttpServletRequest request) {
        PageData pd = this.getPageData();
        return service.updateSkje(pd);
    }
}

